import { regexp } from "./regexp.js";

const { isPx, isEm, isRem, isVw, isVh, isPercent, isRpx } = regexp;

export const isString = (val) => typeof val === "string";

export const isBool = (val) => typeof val === "boolean";

export const isNumber = (val) => typeof val === "number";

export const sliceValue = (val, index) => val && val.slice(0, index);

export const isObject = (val) => val !== null && typeof val === "object";

export const toCalTransformationSize = (value) => {
  if (isNumber(value)) {
    return value;
  }

  if (isRpx(value)) {
    return +value.replace("rpx", "") * 2;
  }

  if (isPx(value)) {
    return +value.replace("px", "");
  }

  if (isVw(value)) {
    return (+value.replace("vw", "") * window.innerWidth) / 100;
  }

  if (isVh(value)) {
    return (+value.replace("vh", "") * window.innerHeight) / 100;
  }

  if (isRem(value)) {
    const num = +value.replace("rem", "");
    const docuFontSize = window.getComputedStyle(
      document.documentElement
    ).fontSize;
    return num * parseFloat(docuFontSize);
  }

  if (isString(value)) {
    return Number(value);
  }
  return 0;
};

export const toCalSize = (value) => {
  if (value === null) {
    return undefined;
  }
  console.log(isRpx(value), value);
  if (
    isPercent(value) ||
    isVw(value) ||
    isVh(value) ||
    isRem(value) ||
    isEm(value) ||
    isRpx(value)
  ) {
    return value;
  }

  return `${toCalTransformationSize(value)}`;
};
